﻿Public Class xtf_ABMconfig_usuariocolumna 

    Private obj_config_usuariocolumna_ As cls_config_usuariocolumna_
    Private ulg_Listado As DevExpress.XtraGrid.Views.Grid.GridView
    Private str_Modulo As String
    Private int_IDUsuario As Integer

    Private bit_NuevaConfig As Boolean = True

    Public Sub New(ByVal Listado As DevExpress.XtraGrid.Views.Grid.GridView, ByVal Modulo As String)
        InitializeComponent()

        obj_config_usuariocolumna_ = New cls_config_usuariocolumna_
        ulg_Listado = Listado
        str_Modulo = Modulo
        int_IDUsuario = UsuarioLOG.idusuario
        Me.Text = "Mostrar / Ocultar columnas"
        CargarColumnas()
        CargarConfiguracion()
    End Sub

    Private Sub CargarColumnas()
        For i As Integer = 0 To ulg_Listado.Columns.Count - 1
            Dim Elemento As New CustomListItem

            Elemento.text = ulg_Listado.Columns(i).GetTextCaption
            'Check.Name = "chk_" & i
            Elemento.objeto = i
            liv_Visibles.Items.Add(Elemento)
            'Elemento.Appearance.Image = Nothing
            'Check.Checked = True
            'Check.AutoSize = True
            'Check.BackColor = Color.Transparent
            'Check.Margin = New System.Windows.Forms.Padding(0, 5, 3, 5)
            'flp_Columnas.Controls.Add(Check)
        Next
    End Sub

    Private Sub CargarConfiguracion()
        If obj_config_usuariocolumna_.Buscarxidusuarioxmodulo(int_IDUsuario, str_Modulo) Then
            For Each columna As String In obj_config_usuariocolumna_.columnas.Split(New Char() {";"}, StringSplitOptions.RemoveEmptyEntries)
                For i As Integer = liv_Visibles.Items.Count - 1 To 0 Step -1
                    If liv_Visibles.Items(i).objeto = columna Then
                        Dim elemento As CustomListItem = liv_Visibles.Items(i)
                        liv_Visibles.Items.Remove(elemento)
                        liv_Ocultas.Items.Add(elemento)
                    End If
                Next
            Next
            bit_NuevaConfig = False
        End If
        Dim index As Integer = 0
        For Each columna As String In obj_config_usuariocolumna_.columnasOrden.Split(New Char() {";"}, StringSplitOptions.RemoveEmptyEntries)
            For i As Integer = 0 To liv_Visibles.Items.Count - 1
                If liv_Visibles.Items(i).objeto = columna Then
                    Dim elemento As CustomListItem = liv_Visibles.Items(i)
                    liv_Visibles.Items.Remove(elemento)
                    liv_Visibles.Items.Insert(index, elemento)
                    index += 1
                End If
            Next
        Next
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        GuardarCambios()
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub GuardarCambios()
        Dim str_Columnas As String = ""
        Dim str_ColumnasOrden As String = ""
        For Each Elemento As customlistitem In liv_Ocultas.Items
            str_Columnas &= Elemento.objeto & ";"
        Next
        For Each Elemento As CustomListItem In liv_Visibles.Items
            str_ColumnasOrden &= Elemento.objeto & ";"
        Next

        obj_config_usuariocolumna_.columnas = str_Columnas
        obj_config_usuariocolumna_.columnasOrden = str_ColumnasOrden

        If bit_NuevaConfig Then 'Nuevo
            obj_config_usuariocolumna_.idusuario = int_IDUsuario
            obj_config_usuariocolumna_.modulo = str_Modulo
            obj_config_usuariocolumna_.Nuevo()
        Else 'Modificar
            obj_config_usuariocolumna_.Modificar()
        End If
    End Sub

    Private Sub btn_Quitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Quitar.Click
        For i As Integer = 0 To liv_Ocultas.SelectedItems.Count - 1
            Dim Elemento As customlistitem = liv_Ocultas.SelectedItems(0)
            liv_Ocultas.Items.Remove(Elemento)
            liv_Visibles.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_Agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Agregar.Click
        For i As Integer = 0 To liv_Visibles.SelectedItems.Count - 1
            Dim Elemento As customlistitem = liv_Visibles.SelectedItems(0)
            liv_Visibles.Items.Remove(Elemento)
            liv_Ocultas.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_AgregarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_AgregarTodos.Click
        For i As Integer = 0 To liv_Visibles.Items.Count - 1
            Dim Elemento As customlistitem = liv_Visibles.Items(0)
            liv_Visibles.Items.Remove(Elemento)
            liv_Ocultas.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_QuitarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_QuitarTodos.Click
        For i As Integer = 0 To liv_Ocultas.Items.Count - 1
            Dim Elemento As customlistitem = liv_Ocultas.Items(0)
            liv_Ocultas.Items.Remove(Elemento)
            liv_Visibles.Items.Add(Elemento)
        Next
    End Sub

    Private Sub btn_subir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_subir.Click
        If liv_Visibles.SelectedItems.Count > 0 Then
            Dim Sel As Object = liv_Visibles.SelectedItem
            Dim Index As Integer = liv_Visibles.Items.IndexOf(liv_Visibles.SelectedItem)
            If Index > 0 Then
                liv_Visibles.Items.RemoveAt(Index)
                liv_Visibles.Items.Insert(Index - 1, Sel)
                liv_Visibles.SelectedItem = Sel
            End If
        End If
    End Sub

    Private Sub btn_Bajar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Bajar.Click
        If liv_Visibles.SelectedItems.Count > 0 Then
            Dim Sel As Object = liv_Visibles.SelectedItem
            Dim Index As Integer = liv_Visibles.Items.IndexOf(liv_Visibles.SelectedItem)
            If Index < liv_Visibles.Items.Count - 1 Then
                liv_Visibles.Items.RemoveAt(Index)
                liv_Visibles.Items.Insert(Index + 1, Sel)
                liv_Visibles.SelectedItem = Sel
            End If
        End If
    End Sub
End Class